import { createRouter, createWebHistory } from "vue-router";
import useUserStore from "../stores/userStore";
//导入组件
import LoginVue from '@/views/login.vue'
import LayoutVue from '@/views/admin/Layout.vue'
import CaseStatisticsVue from '@/views/admin/manage/case/CaseStatistics.vue'
import CaseBacklogVue from '@/views/admin/manage/case/CaseBacklog.vue'
import CaseCompleted from '@/views/admin/manage/case/CaseCompleted.vue'
import CaseUnCompleted from '@/views/admin/manage/case/CaseUnCompleted.vue';

import AdminsVue from '@/views/admin/manage/personnel/Admins.vue'
import ApplicantsVue from '@/views/admin/manage/personnel/Applicants.vue'
import AssistantsVue from '@/views/admin/manage/personnel/Assistants.vue'
import MediatorsVue from '@/views/admin/manage/personnel/Mediators.vue'
import RespondentsVue from '@/views/admin/manage/personnel/Respondents.vue'

import ApplicationInsVue from '@/views/admin/manage/organization/ApplicationIns.vue'
import MediatorsOrgVue from '@/views/admin/manage/organization/MediatorsOrg.vue'

import UserInfoVue from '@/views/admin/user/UserInfo.vue'

import MediatorIndex from '@/views/mediator/index.vue'
import MediatorFirstPage from "@/views/mediator/firstPage.vue";
import paymentHistory from "@/views/mediator/paymentHistory.vue";
import applicant from "../views/applicant/applicant.vue";
import applicantHistory from "../views/applicant/myhistory.vue";
import applicantApply from "../views/applicant/apply.vue";
import repayment from "../views/applicant/repayment.vue";
import mediatorMessagePage from "@/views/mediator/mediatorMessage.vue";
import caseUnCompletedPage from"@/views/mediator/caseUnCompleted.vue";
import caseCompletedPage from "@/views/mediator/caseCompleted.vue";
import Layout from '@/views/assistant/Layout.vue';
import AssignRespondent from '@/views/assistant/AssignRespondent.vue';
import AssignTime from '@/views/assistant/AssignTime.vue';
import UserType from "../types/userType";

import CaseReject from "@/views/admin/manage/case/CaseReject.vue";
import Notice from "@/utils/notice";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/login',
      component:LoginVue
    },
    {
        path:'/admin',
        component:LayoutVue,
        redirect:'/admin/manage/case/statistics',
        children:[
            {path:'/admin/manage/case/statistics',component:CaseStatisticsVue},
            {path:'/admin/manage/case/backlog',component:CaseBacklogVue},
            {path:'/admin/manage/case/completed',component:CaseCompleted},
            {path:'/admin/manage/case/unCompleted',component:CaseUnCompleted},
            {path:'/admin/manage/case/inject',component:CaseReject},
            {path:'/admin/manage/personnel/admins',component:AdminsVue},
            {path:'/admin/manage/personnel/applicants',component:ApplicantsVue},
            {path:'/admin/manage/personnel/assistants',component:AssistantsVue},
            {path:'/admin/manage/personnel/mediators',component:MediatorsVue},
            {path:'/admin/manage/personnel/respondents',component:RespondentsVue},
            {path:'/admin/manage/organization/applicationIns',component:ApplicationInsVue},
            {path:'/admin/manage/organization/mediatorsOrg',component:MediatorsOrgVue},
            {path:'/admin/user/info',component:UserInfoVue},
        ],
    },
    {
      path: '/mediator',
      redirect:'/mediator/firstPage',
      component: MediatorIndex,
      children:[
        {
          path:'/mediator/firstPage',
          component:MediatorFirstPage,
        },{
          path:'/mediator/userMessage',
          component:mediatorMessagePage,
        },{
          path:"/mediator/caseCompleted",
          component:caseCompletedPage
        },{
          path:"/mediator/caseUnCompleted",
          component:caseUnCompletedPage
        },{
          path:"/mediator/paymentHistory",
          component:paymentHistory
        }
      ]
    },
      {
          path:'/respondent',
          redirect:'/respondent/mediating',
          //@ts-ignore
          component:()=>import('@/views/respondent/index.vue'),
          children:[
              //@ts-ignore
              {path:'unfinished',component:()=>import('@/views/respondent/respondent-unfinished-case.vue')},
              //@ts-ignore
              {path:'completed',component:()=>import('@/views/respondent/respondent-completed-case.vue')},
              //@ts-ignore
              {path:'mediating',component:()=>import('@/views/respondent/respondent-mediating-case.vue')},
              //@ts-ignore
              {path:'profile',component:()=>import('@/views/respondent/respondent-info-view.vue')},
              //@ts-ignore
              {path:'change-password',component:()=>import('@/views/respondent/change-password-view.vue')}
          ]
      },
      ,
    {
      path: '/applicant',
      component: applicant,
      redirect:'/applicant/history',
      children:[
        {path:'/applicant/apply',name:"appApply",component:applicantApply},
        {path:'/applicant/history',name:"appHistory",component:applicantHistory},
        {path:'/applicant/repayment',name:"appRepayment",component:repayment}
      ]
    },
    {
      path: '/assistant',
      component: Layout,
      redirect: '/assistant/assign-respondent',
      children: [
        {
          path: 'assign-respondent',
          name: 'AssignRespondent',
          component: AssignRespondent,
          meta: { title: '分配被申请人' }
        },
        {
          path: 'assign-time',
          name: 'AssignTime',
          component: AssignTime,
          meta: { title: '分配调解时间' }
        }
      ]
    },{
      path: '/',
      redirect: '/login',
    }
  ],
});
let commonPath=["/login"];
let routerConfig={
  开启路由守卫:true
}
let isCommonPath=(uri:string)=>commonPath.includes(uri);

router.beforeEach(async (to, from) => {
  if(!routerConfig.开启路由守卫) return true;
  let userStore=useUserStore();
  let userType=userStore.userType;
  if(isCommonPath(to.path)){
     document.title="法院调解系统"
    return true;
  } 
  if(userType==UserType.admin&&to.path.startsWith("/admin")){
        document.title="管理员界面"
        return true;
  }
  if(userType==UserType.mediator&&to.path.startsWith("/mediator")){
        document.title="调解员界面"
      return true;
  }
  if(userType==UserType.respondent&&to.path.startsWith("/respondent")){
      document.title="被申请人界面"
      return true;
  }
  if(userType==UserType.assistant&&to.path.startsWith("/assistant")){
    document.title="助理人员界面"
      return true;
  }
  if(userType==UserType.applicant&&to.path.startsWith("/applicant")){
    document.title="申请人界面"
    return true;
  }
  Notice.error("无权限访问");
  document.title="法院调解系统"
  return {
    path: '/login',
  }
})
export default router;
